
package com.bbh.plugin.single.privilege.demo.controller;

import com.bbh.plugin.single.privilege.core.entity.Employee;
import com.bbh.plugin.single.privilege.core.enums.BaseEnum;
import com.bbh.plugin.single.privilege.core.service.EmployeeService;
import com.bbh.plugin.single.privilege.core.service.OrganizationService;
import com.bbh.plugin.single.privilege.core.vo.EmployeeVo;
import com.bbh.common.base.vo.GridVo;
import com.bbh.common.domain.Condition;
import com.bbh.common.domain.Message;
import com.bbh.common.domain.Page;
import com.bbh.common.domain.StatusType;
import com.bbh.common.utils.IdUtils64;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;


@Controller
@RequestMapping("/employee")
public class EmployeeController extends BasicController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private OrganizationService organizationService;

    @RequestMapping(value = VIEW_FILED + "/index", method = RequestMethod.GET)
    public String index(ModelMap model) {
        return "single/employee/index";
    }


    @RequestMapping(value = VIEW_FILED + "/list")
    public
    @ResponseBody
    GridVo list(HttpServletRequest request, String companyCode, String orgCode) throws ServletRequestBindingException {
        Condition condition = new Condition();
        condition.put("dataStatus", BaseEnum.DATA_STATUS.NORMAL.getKey());
        if (StringUtils.isNotEmpty(companyCode)) {
            condition.put("companyCode", companyCode);
        }
        if (StringUtils.isNotEmpty(orgCode)) {
            condition.put("orgCode", orgCode);
        }
        Page<EmployeeVo> pageResult = employeeService.selectPageList(condition, initPageRequest(request, PAGE_NUMBER, PAGE_ROWS));
        return new GridVo(pageResult);
    }


    /**
     * 跳转到edit页面
     *
     * @return
     */
    @RequestMapping(UPDATE_FILED + "/edit")
    public String edit(String id, Model model, String type) {

        if ("edit".equals(type)) {
            Employee employee = employeeService.selectByPrimaryKey(id);
            model.addAttribute("employee", employee);
            model.addAttribute("organization", organizationService.selectByPrimaryKey(employee.getOrganizationId()));
            model.addAttribute("company", organizationService.selectByPrimaryKey(employee.getCompanyId()));
            model.addAttribute(OPERATE_TYPE, "edit");
        }
        if ("add".equals(type)) {
            model.addAttribute(OPERATE_TYPE, "add");
        }
        return "single/employee/edit";
    }


    @RequestMapping(value = INSERT_FILED + "/insert")
    public
    @ResponseBody
    Message insert(@Valid Employee employee) {
        Condition condition = new Condition();
        if (StringUtils.isNotBlank(employee.getNo())) {
            condition.putEquals("no", employee.getNo());
        }


        Date date = new Date();

        employee.setCreateTime(date);
        employee.setModifyTime(date);
        employee.setCreateBy(getLoginName());
        employee.setModifyBy(getLoginName());
        employee.setDataStatus(BaseEnum.DATA_STATUS.NORMAL.getKey());
        employee.setId(IdUtils64.getInstance().getStringId());
        employeeService.insert(employee);

        return new Message(StatusType.SUCCESS.getKey(), "增加成功");
    }

    /**
     * update
     *
     * @return
     */
    @RequestMapping(UPDATE_FILED + "/update")
    public
    @ResponseBody
    Message update(@Valid Employee employee, String employeeGroupIds) {
        Assert.notNull(employee.getId(), "参数错误");
        Employee modifyEmoloyee = employeeService.selectByPrimaryKey(employee.getId());
        if (modifyEmoloyee != null) {
            Date date = new Date();
            modifyEmoloyee.setCompanyId(employee.getCompanyId());
            modifyEmoloyee.setOrganizationId(employee.getOrganizationId());
            modifyEmoloyee.setNo(employee.getNo());
            modifyEmoloyee.setName(employee.getName());
            modifyEmoloyee.setIsEnabled(employee.getIsEnabled());
            modifyEmoloyee.setModifyBy(getLoginName());
            modifyEmoloyee.setModifyTime(date);
            modifyEmoloyee.setDescription(employee.getDescription());
            modifyEmoloyee.setEmail(employee.getEmail());
            modifyEmoloyee.setPhone(employee.getPhone());
            modifyEmoloyee.setMobile(employee.getMobile());
            modifyEmoloyee.setSort(employee.getSort());
            employeeService.updateByPrimaryKeySelective(modifyEmoloyee);
            return new Message(StatusType.SUCCESS.getKey(), "更新成功");
        }
        return new Message(StatusType.WARN.getKey(), "参数错误");
    }


    @RequestMapping(DELETE_FILED + "/delete")
    public
    @ResponseBody
    Message delete(@RequestParam(value = "ids[]") String[] ids) {
        if (ArrayUtils.isNotEmpty(ids)) {
            employeeService.delete(ids);
            return new Message(StatusType.SUCCESS.getKey(), "删除成功");
        }
        return null;
    }

    /**
     * 用户关联角色界面
     *
     * @return
     */
    @RequestMapping(VIEW_FILED + "/authorization/edit")
    public String authorizationEdit(HttpServletRequest request, Model model) throws ServletRequestBindingException {
        model.addAttribute("id", request.getParameter("id"));
        return "single/employee/authorization";
    }


}